import { MigrationInterface, QueryRunner } from 'typeorm'

export class AddWorkWallTables1747096000000 implements MigrationInterface {
  name = 'AddWorkWallTables1747096000000'
  
  public async up(queryRunner: QueryRunner): Promise<void> {
    // 创建主计划表
    await queryRunner.query(`
      CREATE TABLE \`c_main_plan\` (
        \`id\` int NOT NULL AUTO_INCREMENT,
        \`created_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
        \`updated_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
        \`is_delete\` int NOT NULL DEFAULT '0',
        \`create_by\` int NULL COMMENT '创建者',
        \`update_by\` int NULL COMMENT '更新者',
        \`plan_id\` varchar(255) NOT NULL COMMENT '计划ID, PL+yymmdd+000',
        \`plan_name\` varchar(255) NOT NULL COMMENT '计划名称',
        \`class_id\` varchar(255) NOT NULL COMMENT '班级ID',
        \`mark\` varchar(255) NOT NULL DEFAULT '' COMMENT '计划备注',
        \`progress\` float NOT NULL DEFAULT '0' COMMENT '计划进度',
        \`publish_at\` datetime NULL COMMENT '发布时间',
        \`complete_at\` datetime NULL COMMENT '完成时间',
        \`punch_start_at\` datetime NULL COMMENT '打卡开始时间',
        PRIMARY KEY (\`id\`),
        UNIQUE INDEX \`IDX_plan_id\` (\`plan_id\`)
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
    `);

    // 创建子计划表
    await queryRunner.query(`
      CREATE TABLE \`c_sub_plan\` (
        \`id\` int NOT NULL AUTO_INCREMENT,
        \`created_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6),
        \`updated_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
        \`is_delete\` int NOT NULL DEFAULT '0',
        \`create_by\` int NULL COMMENT '创建者',
        \`update_by\` int NULL COMMENT '更新者',
        \`plan_id\` int NOT NULL COMMENT '主计划ID',
        \`sub_plan_id\` varchar(255) NOT NULL COMMENT '子计划ID, SPL+yymmdd+000',
        \`sub_plan_name\` varchar(100) NOT NULL COMMENT '子计划名称',
        \`sub_plan_mark\` varchar(100) NOT NULL COMMENT '子计划备注',
        PRIMARY KEY (\`id\`),
        UNIQUE INDEX \`IDX_sub_plan_id\` (\`sub_plan_id\`),
        INDEX \`IDX_plan_id\` (\`plan_id\`),
        CONSTRAINT \`FK_sub_plan_main_plan\` FOREIGN KEY (\`plan_id\`) REFERENCES \`c_main_plan\` (\`id\`) ON DELETE CASCADE
      ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
    `);
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    await queryRunner.query(`DROP TABLE \`c_sub_plan\``);
    await queryRunner.query(`DROP TABLE \`c_main_plan\``);
  }
} 